package org.hanbo.code.repository.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.hanbo.code.repository.exception.CodeSnippetActionNotAuthorizedException;
import org.hanbo.code.repository.exception.CodeSnippetExceptionBase;
import org.hanbo.code.repository.exception.CodeSnippetNotFoundException;
import org.hanbo.code.repository.exception.ErrorDescription;
import org.hanbo.code.repository.persistence.entity.CodeSnippet;
import org.hanbo.code.repository.persistence.entity.CodeSnippetDAO;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class DeleteCodeSnippetAction
extends CodeSnippetActionBase implements ServletRequestAware
{
	private static final long serialVersionUID = 9116754476337804104L;
	
	private HttpServletRequest servletReq;
	private ErrorDescription errorDescription;
	
	private long articleId;
	private String articleName;

	public void setServletRequest(HttpServletRequest arg0)
	{
		servletReq = arg0;
	}
	
	public String execute()
	{
		if (!checkLogin())
		{
			return super.getCurrentStageValue();
		}
		
		if (!super.checkRegistration())
		{
			return super.getCurrentStageValue();
		}

		String loggedInUserEmail = super
			.getRegisteredUser()
			.getUserEmail()
			.getEmail();
		
		try
		{
			if(servletReq != null)
			{
				String requestType = servletReq.getParameter("type");
				String retrievalId = servletReq.getParameter("id");
				
				if (StringUtils.isEmpty(requestType) || StringUtils.isEmpty(retrievalId))
				{
					throw new IllegalArgumentException(
						"Request does not contain \"type\" or \"id\" for searching code snippet"
					);
				}
				
				long actualId = -1;
				try
				{
					actualId = Long.parseLong(retrievalId);
				}
				catch(NumberFormatException e)
				{
					throw e;
				}
				
				Key codeSnippetId = KeyFactory.createKey(requestType, actualId);
				CodeSnippetDAO codeSnippetRetriever = new CodeSnippetDAO();
				try
				{
					codeSnippetRetriever.acquirePersistenceManager();
					CodeSnippet snippet = codeSnippetRetriever.getCodeSnippetById(codeSnippetId);
					if (snippet == null)
					{
						throw new CodeSnippetNotFoundException(actualId);
					}
					
					articleId = snippet.getId().getId();
					articleName = snippet.getSubject();
					
					String authorEmailField = snippet.getAuthorEmail();
					if (authorEmailField.equals(loggedInUserEmail))
					{
						codeSnippetRetriever.deleteCodeSnippetById(codeSnippetId);
					}
					else
					{
						throw new CodeSnippetActionNotAuthorizedException(
							"Delete code snippet not belong to you."
						);
					}
				}
				catch (Exception e)
				{
					throw e;
				}
				finally
				{
					codeSnippetRetriever.releasePersistenceManager();
				}
			}
		}
		catch(CodeSnippetExceptionBase e)
		{
			errorDescription = new ErrorDescription(e);
			return ERROR;
		}
		catch(Exception e)
		{
			errorDescription = new ErrorDescription(1000, e);
			return ERROR;
		}
		return SUCCESS;
	}

	public ErrorDescription getErrorDescription()
	{
		return errorDescription;
	}
	
	public String getNextActionPage()
	{
		return "/index.action";
	}

	public String getRedirectDisplayMessage()
	{
		return "Your submission ["
			+ this.articleName
			+ "] (id =["
			+ this.articleId
			+ "]) has been deleted.";
	}
}
